ElasticSearch JAVA API Official document: Https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.htmlFirst, generate JSONThe first step in creating an index is to convert the object to a JSON string. There are four ways to create JSON documents:1.1 Handwriting Style generationString json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" +
congratulations, now you can build es stand-alone and Es clusters, everything is so simple.
Let's take a look at the ES of the HTTP API insert, DELETE, update, find, search function, (es installed in the Ubuntu Server 64-bit 12.04LTS). Insert
Let's start with a simple official example, insert a parameter for-xput, and insert a record.
$ Curl-xput ' HTTP://LOCALHOST:9200/TWITTER/TWEET/1 '-d ' {
"user": "Kimchy",
"post_date": "2009-11-15t14 : 1
Elasticsearch's Javaapi query dsl-queriesLike the rest query dsl ,elasticsearch provides a complete Java query DSL. The Query Builder (factory) is QueryBuilders . Once you have your query ready , You can use the query API.How to useQueryBuilders?仅仅就是加入下面的包:Import org . Elasticsearch . Index . Query . querybuilders .*;Note that you can easily print ( aka Debug ) The JSONgenerated by the query, using the toString() methodAny API that can be used to accept queries , such as Count and. QueryBuilde
1, full-text query overview
Https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-full-text-queries.html
The high-level full text queries are usually used to running full text queries on full text fields like the ' body of ' an EM Ail. They understand how the field being queried are analyzed and would apply each field ' analyzer (or Search_analyzer) to the Q Uery string before executing. 1.1 Match Query
The standard query for performing full text queries, including fuzzy matchi
(). Addtransportaddress (NewInetsockettransportaddress (NewInetsocketaddress ("192.168.50.37", 9300))); //Default 5s//how long to open the connection, default 5sSYSTEM.OUT.PRINTLN ("Success Connect"); }2. Using QueryBuilder for queryingUsing QueryBuilder2.1 Termquery ("key", obj) exactly matches2.1 Termsquery ("Key", Obj1, obj2 ...) matches multiple values at once2.3 Matchquery ("key", OBJ) single match, field does not support wildcard characters, prefix with advanced features2.4 Multimatchquer
Index APIOriginal: http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.htmlThe index API allows you to convert a JSON document to a specific index, making it easy to search for operations.To generate a JSON document:There are several different ways to generate a JSON document:
Manual use byte[] orString
Use a map to convert the equivalent to JSON
use a third-party library to reload beans (such as Jackson)
Using the built-in Xcontentfactory.js
Elasticsearch REST APIElasticsearch supports HTTP request-response service, HTTP requests are broken by default with 9200, so the Curl command allows you to send an HTTP request and get the JSON return content. Common rest APIs include a few:Check ES cluster statuscurl http://localhost:9200/_cat/health?vCheck the status of the ES nodecurl http://localhost:9200/_cat/nodes?vQuerying all the Indexescurl http://localhost:9200/_cat/indices?vCreate an indexcurl -XPUT http://localhost:9200/myindex/myty
Elasticsearch supports batch addition or deletion of index documents. In Java APIs, bulkrequestbuilder is constructed, batch index/delete requests are added to bulkrequestbuilder, and bulkrequestbuilder is executed. The following is an example:
Import static Org. elasticsearch. common. xcontent. xcontentfactory. *; bulkrequestbuilder bulkrequest = client. preparebulk (); bulkrequest. add (client. prepareindex ("Twitter", "tweet", "1 "). setsource (jsonbuilder (). startobject (). field ("user", "
Bulk APIThe bulk API allows developers to index and delete multiple documents in a single request. Here is the usage instance.ImportStatic org.elasticsearch.common.xcontent.xcontentfactory.*; Bulkrequestbuilder bulkrequest = Client.preparebulk ();Either use Client#prepare, or use requests# to directly build Index/delete Requestsbulkrequest.add (client.prepareindex ("Twitter","Tweet","1"). SetSource (Jsonbuilder (). StartObject (). Field ("User","Kimchy
/elasticsearch data:/var/data/elasticsearch Plugins:/var/data/plugins conf :/etc/confSo we can see that we output the log to/var/logs/elasticsearch, save the index data in the/var/data/elasticsearch directory, the plug-in location is set to/var/data/plugins, The configuration file drop location is set to/etc/conf.Of course, we can also directly path.logs:/var/logs/elasticsearch this configuration2.2 Configuring the cluster name, node nameCluster:name:myclusterNode:name:mynodeThis sets the cluste
?routing=kimchy '-d ' {"Query": {"Filtered": {"Query": {"Query_string": {"Query": "Some query string here"}},"Filter": {"term": {"user": "Kimchy"}}}}}'//This format is no longer supported3. Multi-field search (Multi-fields)The new format is as follows:"title": {' Type ': ' String ',"Fields": {"Raw": {"type": "string", "index": "Not_analyzed"}}}4. Stop WordThe new version does not use the English default sto
1. Search Api1.1.routing: Routingwhen you perform a search, it broadcasts to all index/index shards (loops between replicas). You can routing control which shards will be searched by providing parameters . For example, when you index a tweet, the route value can be a user name. post/twitter/tweet?routing=kimchy{ " user ": kimchy " postdate ": " 2009-11-15t14:12:12 " , " : " trying ou
of course be used in query mode.Next, let's configure the NLog log to output to ElasticSearch, first install the package NLog.Targets.ElasticSearch 1.0.14Pm> Install-package NLog.Targets.ElasticSearchThe corresponding nlog.config file is like this, look at the bold font:This allows us to freely output non-anomalous logs to elasticsearch, such as the logs we recorded for the WEBAPI request:Devlogging is the index name that we have configured in the configuration file. We also use Nlog to record
, operationThreaded The actions that are set to true , This mean that the operation is performed on a different thread are as follows an example, set to False:
Deleteresponse response = Client.preparedelete ("Twitter", "tweet", "1")
. setoperationthreaded (False)
. Execute ()
. Actionget ();
The bulk of the javaapi of Elasticsearch
The Bulk API can be used to retrieve and delete multiple data in a single request, and here is an example:
Import
Static
org
.
Elasticsea
Elasticsearch queries query conditions in JSON format. in Java APIs, Query Builder objects are constructed. elasticsearch fully supports querydsl-style queries. The construction class of querybuilder is querybuilders, the filter build class is filterbuilders. The following is an example of constructing querybuilder:
Import static Org. elasticsearch. index. query. filterbuilders. *; import static Org. elasticsearch. index. query. querybuilders. *; querybuilder QB1 = termquery ("name", "
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.